##=============================================================================
## Main Manuscript: Figure 2
##=============================================================================

##-----------------
# clear environment
rm(list=ls())
options(stringsAsFactors = FALSE, scipen = 999)
# source("R/functions.R")

##--------
# Set seed
seed <- sample.int(.Machine$integer.max, 1)
set.seed(seed)

##-------------
# Load Packages
ipak <- function(pkg){new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
if(length(new.pkg)) install.packages(new.pkg, dependencies = TRUE)
sapply(pkg, require, character.only = TRUE)
}

packages <- c("tidyverse")

ipak(packages)

##---------
# Load Data
#setwd("/Users/austinknuppe/Library/CloudStorage/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/final_version_oct_2024/replication-scripts")
load("clean_ukraine_data.RData")

##-------------
# Plot Figure 2

# num_col <- function(x){
#   any(!is.character(x))
# }
# 
# # Wave 1 (7 = DK, 8 = REF)
# compromise_1 <- haven::read_sav("~/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/Raw_data/Data_OMN_July_2022_AP.sav") %>% 
#   janitor::clean_names() %>%
#   haven::zap_label() %>% 
#   mutate_if(is.character, list(~na_if(., ""))) %>% 
#   mutate_if(num_col, as.numeric) %>% 
#   dplyr::select(v1, v113_1:v113_8) %>% 
#   mutate(across(starts_with("v113_"), ~ifelse(.x == 1, 1, 0))) %>% 
#   mutate(across(starts_with("v113_"), factor, levels = 0:1, 
#                 labels = c("No", "Yes"))) %>% 
#   mutate(wave = 1) %>% 
#   as.data.frame() %>% 
#   rename(id = v1, 
#          comp_1 = v113_1, 
#          comp_2 = v113_2, 
#          comp_3 = v113_3,
#          comp_4 = v113_4, 
#          comp_5 = v113_5, 
#          comp_6 = v113_6, 
#          comp_dk = v113_7,
#          comp_ref = v113_8)
# 
# compromise_2 <- haven::read_sav("~/Dropbox/Ukraine2022WartimeSurvey/Paper_peace/Raw_data/Data_OMN_February_2023_AP.sav") %>% 
#   janitor::clean_names() %>%
#   haven::zap_label() %>% 
#   mutate_if(is.character, list(~na_if(., ""))) %>% 
#   mutate_if(num_col, as.numeric) %>% 
#   dplyr::select(v1, v166_1:v166_8) %>% 
#   mutate(across(starts_with("v166_"), ~ifelse(.x == 1, 1, 0))) %>% 
#   mutate(across(starts_with("v166_"), factor, levels = 0:1, 
#                 labels = c("No", "Yes"))) %>% 
#   mutate(wave = 2) %>% 
#   as.data.frame() %>% 
#   rename(id = v1, 
#          comp_1 = v166_1, 
#          comp_2 = v166_2, 
#          comp_3 = v166_3,
#          comp_4 = v166_4, 
#          comp_5 = v166_5, 
#          comp_6 = v166_6, 
#          comp_dk = v166_7,
#          comp_ref = v166_8) 
# 
# compromise <- bind_rows(compromise_1, compromise_2) %>% 
#   mutate(wave = factor(wave, levels = 1:2, labels = c("Wave 1", "Wave 2"))) %>% 
#   as.data.frame()
# 
# rm(compromise_1, compromise_2)
# 
# compromise <- compromise %>% 
#   #filter(comp_dk == "No") %>% 
#   filter(comp_ref == "No") %>% 
#   as.data.frame() %>% 
#   rename(
#     "Russian control over Crimea" = comp_1,
#     "Russian control over the territories of the so-called DNR and LNR 'republics'" = comp_2,
#     "Russian control over the Donetsk and Luhansk oblasts" = comp_3,
#     "Russian control over Kherson oblast and the land-based corridor to Crimea" = comp_4,
#     "A change to Ukraine's constitution to remove the amendment on adherence to NATO accession and proclaim Ukraine's neutral military status ('out-of-bloc status')" = comp_5,
#     "The return of Russian language-based secondary education in Ukraine to pre-2020 levels" = comp_6)

mylikert <- likert::likert(compromise[, 2:7], grouping = compromise$wave)
myplot <- plot(mylikert, colors = c("gray40", "gray20"))

newplot <- myplot + facet_wrap(~factor(Item, levels =  c("A change to Ukraine's constitution to remove the amendment on adherence to NATO accession and proclaim Ukraine's neutral military status ('out-of-bloc status')",
                                                         "The return of Russian language-based secondary education in Ukraine to pre-2020 levels", 
                                                         "Russian control over Crimea", 
                                                         "Russian control over the territories of the so-called DNR and LNR 'republics'",
                                                         "Russian control over the Donetsk and Luhansk oblasts",
                                                         "Russian control over Kherson oblast and the land-based corridor to Crimea")))

newplot

plot(mylikert, colors = c("gray40", "gray20")) +
  scale_y_continuous(labels = likert:::abs_formatter, lim = c(-100, 100),
                     breaks = seq(-100, 100, 25)) +
  theme(text = element_text(size = 16))

rm(list = ls())
##=============================================================================
## End of File
##=============================================================================